#include <stdio.h>
#include <string.h>

//用char数组存储2-Bitmap，不用考虑大小端内存的问题
unsigned char flags[1000];//数组大小自定义

//得到idx的统计个数，分别为0,1,2
unsigned int get_val(int idx)
{
	//|	   8 bit   |
	//|00 00 00 00 | 表示3 2 1 0
	//|00 00 00 00 | 表示7 6 5 4
	//...
	//|00 00 00 00 |
	int i=idx/4;
	int j=idx%4;
	unsigned int ret = (flags[i]&(0x3<<(2*j)))>>(2*j);

	return ret;
}

//设置idx的统计个数，将idx的统计个数设置为val
unsigned int set_val(int idx, unsigned int val)
{
	int i=idx/4;
	int j=idx%4;
	unsigned int tmp = (flags[i]&~((0x3<<(2*j))&0xff)) | (((val%4)<<(2*j))&0xff);
	flags[i] = tmp;
	return 0;
}

//
unsigned int add_one(int idx)
{
	if(get_val(idx)>=2)
	{
		return 1;
	}
	else
	{
		set_val(idx, get_val(idx)+1);
		return 0;
	}
}

int a[] = {1,3,5,7,9,1,3,5,7,1,3,5,1,3,1,10,2,4,6,8,0};

int main()
{
	int i;
	memset(flags, 0, sizeof(flags));

	printf("原数组为：");
	for(i=0; i<sizeof(a)/sizeof(int); ++i)
	{
		printf("%d ", a[i]);
		add_one(a[i]);
	}
	printf("\r\n");

	printf("只出现过一次的数：");
	for(i=0; i<100; ++i)
	{
		if(get_val(i)==1)
		{
			printf("%d ", i);
		}
	}
	printf("\r\n");

	return 0;
}
